<template>
  <div>
    <img src="../assets/bbb.png" alt class="flagimg" />
    <div class="login_container">
      <div class="login_box">
        <!-- 头像区域 -->
        <div class="avater_box">
          <img src="../assets/timg.jpg" alt class="img" />
        </div>
        <!-- 登录表单区域 -->
        <el-form
          ref="loginFormRef"
          label-width="0px"
          class="loginForm"
          :model="loginForm"
          :rules="loginRules"
        >
          <!-- 用户名区域 -->
          <el-form-item prop="username">
            <el-input
              prefix-icon="iconfont icon-touxiang"
              v-model="loginForm.username"
              placeholder="请输入用户名..."
            ></el-input>
          </el-form-item>
          <!-- 密码区域 -->
          <el-form-item prop="password">
            <el-input
              type="password"
              prefix-icon="iconfont icon-icon-"
              v-model="loginForm.password"
              placeholder="密码"
            ></el-input>
          </el-form-item>
          <!-- 按钮区域 -->
          <el-checkbox v-model="checked">记住我</el-checkbox>
          <el-form-item class="btns">
            <el-button type="primary" @click="login()" style="width:60%;">登录</el-button>
            <el-button type="commit" @click="reset" style="width:37%;">重置</el-button>
          </el-form-item>
          <el-row style="text-align:center;margin-top:-10px;">
            <el-link type="primary">忘记密码</el-link>&nbsp;
            <el-link type="primary">
              <router-link to="/register">立即注册</router-link>
            </el-link>
          </el-row>
          <el-row class="otherlogin">
            <hr />
            <img src="../assets/ccc.png" alt style="width:170px;margin-top:10px;cursor:pointer;" />
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //登录表单的绑定数据对象
      loginForm: {
        username: "",
        password: "",
        shenfen: "admin"
      },
      //数据验证规则对象
      loginRules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 10, message: "长度在 6 到 10 个字符", trigger: "blur" }
        ]
      },
      checked: ""
    };
  },
  methods: {
    // 重置表单按钮
    reset() {
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate(valid => {
        if (!valid) return;
        var params = new URLSearchParams();
        params.append("username", this.loginForm.username);
        params.append("pwd", this.loginForm.password);
        params.append("shenfen", this.loginForm.shenfen);
        this.$http.post('/client/login/finish', params).then(res => {
          //  window.console.log(res)
          if (res.data.msg == "登陆失败")
            return this.$message.error("登陆失败，账户或密码错误");
          this.$message.success("登陆成功");
          this.$store.commit('addToken',res.data.token)
          this.$store.commit('addUserId',this.loginForm.username)
          // window.console.log(this.$store.state.token)
          this.$router.push("index");
        })
      })
    },
    changeFlag(){
    //如果有token就改变flag值
    this.token=this.$store.state.token;
    if(!this.token) return
    this.$store.commit('changeFlag')
    }
  },
  destroyed(){
      this.changeFlag()
      sessionStorage.setItem('state', JSON.stringify(this.$store.state))
  }
}
</script>

<style scoped>
.flagimg {
  float: left;
  margin: 25px;
  width: 250px;
}
.login_container {
  height: 100%;
  width: 100%;
  background-color: #f1f1f1;
}
.login_box {
  width: 425px;
  height: 400px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  top: 55%;
  left: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 2px 2px #999;
}
.avater_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}
.img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.loginForm {
  position: absolute;
  width: 100%;
  top: 80px;
  padding: 0 20px;
  box-sizing: border-box;
}
.otherlogin {
  color: black;
  text-align: center;
}
hr {
  color: black;
  line-height: 0px;
}
hr::after {
  content: "社交账户登陆";
  font-size: 12px;
}
.el-el-checkbox {
  font-size: 20px;
  color: #999;
}
</style>